﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Eneta.Portal.Common;
using Eneta.Portal.Common.Dto;
using Eneta.Portal.Common.Helpers;
using Eneta.Portal.Common.Repository;
using Eneta.Portal.Controls;
using Microsoft.SharePoint;

namespace Eneta.Portal.NewsAndBlogs.WebParts
{
    public partial class NewsAndBlogsWebPartControl : UserControl
    {
        protected Repeater _informationArea;
        protected Repeater _newsWithSummary;
        protected Repeater _newsWithTitle;
        protected Repeater _blogBoxFirst;
        protected Repeater _blogBox;
        protected Repeater _tools;

        protected HtmlAnchor headerNewsSiteLink;
        protected HtmlAnchor newsSiteLink;
        protected HtmlAnchor announcementsSiteLink;
        protected HtmlAnchor blogSiteLink;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _informationArea.ItemDataBound += InformationAreaItemDataBound;
            _newsWithSummary.ItemDataBound += InformationAreaItemDataBound;
        }

        protected void InformationAreaItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.DataItem == null)
                return;
            //var item = (DataRowView)e.Item.DataItem;
            var item = (NewsItemDto)e.Item.DataItem;

            var keywordsField = (HtmlGenericControl)e.Item.FindControl("keywordsField");
            var dateAndBlogField = (HtmlGenericControl)e.Item.FindControl("dateAndBlogField");

            if (item.Keywords != null && item.Keywords.Count > 0)
            {
                var keywords = (from k in item.Keywords
                                select k.Title).ToArray();
                keywordsField.InnerText = string.Join(", ", keywords);
            }
            else
                keywordsField.InnerText = string.Empty;

            var bodyField = (HtmlGenericControl)e.Item.FindControl("bodyField");
            if (bodyField != null)
            {
                var body = item.Body;  //item["Summary"].ToString();
                body = RegExHelper.RemoveHtmlTags(body).Trim();
                if (body.Length > 255)
                    body = body.Substring(0, 255);
                if (body.StartsWith("&#160;"))
                    body = body.Substring(5).Trim();
                bodyField.InnerText = body;
            }
            dateAndBlogField.InnerText = string.Empty;

            var commentsContainer = e.Item.FindControl("commentsContainer");
            commentsContainer.Visible = item.Comments.Count > 0;

            if (item.PublishDate != DateTime.MinValue)
            {
                dateAndBlogField.InnerText = item.PublishDate.ToString("dd.MM.yyyy");
            }
            else
            {
                dateAndBlogField.InnerText = item.Created.ToString("dd.MM.yyyy");
            }

            if (!string.IsNullOrEmpty(item.InfoSourceName))
            {
                if (!string.IsNullOrEmpty(dateAndBlogField.InnerText))
                    dateAndBlogField.InnerText += " | ";
                dateAndBlogField.InnerText += item.InfoSourceName;
            }
            else
            {
                if (!string.IsNullOrEmpty(dateAndBlogField.InnerText))
                    dateAndBlogField.InnerText += " | ";
                dateAndBlogField.InnerText += "ENETA";
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            headerNewsSiteLink.HRef = EnetaApplication.PortalNewsSiteUrl;
            newsSiteLink.HRef = EnetaApplication.PortalNewsSiteUrl;
            announcementsSiteLink.HRef = EnetaApplication.PortalAnnouncementsSiteUrl;
            blogSiteLink.HRef = EnetaApplication.PortalBlogSiteUrl;

            if (SPContext.Current.Web.CurrentUser != null)
            {
                var blogPromo = FindControl("BlogPromo") as HtmlDisplayControl;
                if (blogPromo != null)
                    blogPromo.Visible = false;
            }

            var blogRepository = new BlogRepository(EnetaApplication.PortalBlogSiteUrl);
            var annRepository = new AnnouncementRepository(EnetaApplication.PortalAnnouncementsSiteUrl);

            var blogs = blogRepository.GetApprovedEntries(1, 12);
            var anns = annRepository.GetApprovedAnnouncements(1, 10);

            _informationArea.DataSource = blogs.Results.Take(2);
            _informationArea.DataBind();

            _blogBoxFirst.DataSource = blogs.Results.Skip(2).Take(5);
            _blogBoxFirst.DataBind();

            _blogBox.DataSource = blogs.Results.Skip(7).Take(5);
            _blogBox.DataBind();

            _newsWithSummary.DataSource = anns.Results.Take(1);
            _newsWithSummary.DataBind();

            _newsWithTitle.DataSource = anns.Results.Skip(1);
            _newsWithTitle.DataBind();
        }
    }
}
